/* 
 * File:   NonGenericList.h
 * Author: Enrique Antezana
 *
 * Created on 9 de abril de 2013, 03:17 PM
 */

#ifndef NONGENERICLIST_H
#define	NONGENERICLIST_H

#include <iostream>
#include <stdio.h>

template <class T>
class Node
{
    T data_;
    Node* nextNode_;

public:

    Node(const T& data)
    {
        data_ = data;
        nextNode_ = NULL;
    };

    ~Node()
    {
        printf("NODE DESTRUCTOR ::~NODE()\n");
    }

    void setData(const T& data)
    {
        data_ = data;
    };

    void setNext(Node* nextNode)
    {
        nextNode_ = nextNode;
    };

    T& getData()
    {
        return data_;
    };

    Node* getNext()
    {
        return nextNode_;
    };
};

template <class T>
class NonGenericList
{
private:
    Node<T>* firstNode_;
    int nodeCounter_;

public:

    NonGenericList();
    void add(const T& data);
    void remove(const T& data);
    int size();
    bool isEmpty();
    T& get(int pos);
    bool contains(const T& data);
};


template <class T>
NonGenericList<T>::NonGenericList()
{
    firstNode_ = NULL;
    nodeCounter_ = 0;
}

template <class T>
void NonGenericList<T>::add(const T& data)
{
    Node<T> *newNode = new Node<T>(data);

    Node<T> *tempNode = firstNode_;

    if (tempNode != NULL)
    {
        while (tempNode->getNext() != NULL)
        {
            tempNode = tempNode->getNext();
        }
        tempNode->setNext(newNode);
        nodeCounter_++;
        //printf("added node with value: %d in position :%d\n", newNode->getData(), nodeCounter_);
    } else
    {
        firstNode_ = newNode;
        nodeCounter_ = 1;
        //printf("first node added with value: %d in position :%d\n", firstNode_->getData(), nodeCounter_);
    }
}

template <class T>

void NonGenericList<T>::remove(const T& data)
{
    Node<T>* tempNode = firstNode_;

    if (tempNode == NULL)return;
    if (tempNode->getData() == data)
    {
        delete tempNode;
        firstNode_ = NULL;
        nodeCounter_--;
    } else
    {
        Node<T> *prevNode;
        do
        {
            if (tempNode->getData() == data) break;
            prevNode = tempNode;
            tempNode = tempNode->getNext();
        } while (tempNode != NULL);
        prevNode->setNext(tempNode->getNext());
        delete tempNode;
        nodeCounter_--;
    }

}

template <class T>

bool NonGenericList<T>::contains(const T& data)
{
    Node<T>* tempNode = firstNode_;

    if (tempNode == NULL)return false;
    if (tempNode->getData() == data)
    {
        return true;
    } else
    {
        Node<T> *prevNode;
        do
        {
            if (tempNode->getData() == data) return true;
            prevNode = tempNode;
            tempNode = tempNode->getNext();
        } while (tempNode != NULL);
        //prevNode->setNext(tempNode->getNext());

    }
    return false;
}

template <class T>

T& NonGenericList<T>::get(int pos)
{

    Node<T>* tempNode = firstNode_;
    int counter = 1;
    if (pos == 1)
    {
        return firstNode_->getData();

    } else
    {
        while (counter < pos)
        {
            Node<T>* nextNode = tempNode->getNext();
            tempNode = nextNode;
            counter++;
        }
        return tempNode->getData();
    }

}

template <class T>

bool NonGenericList<T>::isEmpty()
{
    if (firstNode_ == NULL)
    {
        return true;
    }
    return false;
}

template <class T>

int NonGenericList<T>::size()
{
    return nodeCounter_;
}




#endif	/* NONGENERICLIST_H */

